package com.example.administrator.im.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.administrator.im.R;
import com.example.administrator.im.adapter.IMItemAdapter;
import com.example.administrator.im.config.PhoneParam;
import com.example.administrator.im.data.IMData;
import com.example.administrator.im.db.FriendDBHelper;
import com.example.administrator.im.interfacz.ShareTitleable;
import com.example.administrator.im.ui.activity.SearchFriendActivity;
import com.example.administrator.im.utils.EMUtils;
import com.example.administrator.im.utils.NotificationUtils;
import com.example.administrator.im.view.ShareTitle;
import com.hyphenate.chat.EMMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

import java.util.List;

/**
 * create:2018/9/14 12:11
 * author: 姚鸿杰
 **/
public class IMFragment extends Fragment {


    private final String TAG = "IMFragment";

    private PopupWindow popupWindow;

    private RecyclerView list_animal;
    private IMItemAdapter adapter;

    private ShareTitle shareTitle;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_im, container, false);

        EventBus.getDefault().register(this);

        shareTitle = (ShareTitle) v.findViewById(R.id.im_toolbar);
        shareTitle.setIconOnClickListener(new ShareTitleable() {
            @Override
            public void left_click(View v) {
            }

            @Override
            public void right_click(View v) {
                View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.im_nav, null);

                ViewGroup gourp = (ViewGroup) contentView.findViewById(R.id.group);
                ViewGroup seek_friend = (ViewGroup) contentView.findViewById(R.id.seek_friend);

                gourp.setOnClickListener(new Nav_ClickListener());
                seek_friend.setOnClickListener(new Nav_ClickListener());

                int width = PhoneParam.getInstance(getActivity()).getS_screen_width() / 2;
                int height = v.getHeight();
                int top = v.getTop();
                popupWindow = new PopupWindow(contentView, width,
                        ViewGroup.LayoutParams.WRAP_CONTENT, true);

                popupWindow.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.im_nav_bac, null));

//                popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

                popupWindow.showAsDropDown(v, -width + height, top);
            }
        });

        list_animal = (RecyclerView) v.findViewById(R.id.im_recycler);

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.scrollToPositionWithOffset(0, 0);
        adapter = new IMItemAdapter(getContext(), null);
        list_animal.setLayoutManager(manager);

        View head = LayoutInflater.from(getContext()).inflate(R.layout.head, null);
        adapter.addHeaderView(head);
        list_animal.setAdapter(adapter);

        initData();
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        NotificationUtils.cancelAllNotify();
    }

    class Nav_ClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.group:
                    Toast.makeText(getActivity(), "group", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.seek_friend:
                    getActivity().startActivity(new Intent(getContext(), SearchFriendActivity.class));
                    break;
            }
            //将窗口关闭
            popupWindow.dismiss();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateConversations(UpdateConversations conversation) {
        //更新会话类表
        adapter.updateData(conversation);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setIsAcked(IsAcked isAcked) {
        adapter.setIsAcked(isAcked.getUsername());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateHeadImg(UpdateIMItem img) {
        adapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateIMConversations(UpdateIMConversations conversations) {
        initData();
    }

    private void initData() {
        if (LitePal.findAll(FriendDBHelper.class).size() != 0) {
            List<IMData> mData = EMUtils.getConversations();
            adapter.addItem(mData);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    public static class UpdateConversations {
        private EMMessage msg;
        private boolean isAcked;

        public boolean isAcked() {
            return isAcked;
        }

        public UpdateConversations(EMMessage msg, boolean isAcked) {
            this.msg = msg;
            this.isAcked = isAcked;
        }

        public EMMessage getMsg() {
            return msg;
        }
    }

    public static class IsAcked {
        private String username;

        public IsAcked(String username) {
            this.username = username;
        }

        public String getUsername() {
            return username;
        }
    }

    public static class UpdateIMItem {
    }

    public static class UpdateIMConversations {
    }
}
